package com.zs.service.impl;

import com.zs.dao.RoleDao;
import com.zs.dao.UserDao;
import com.zs.domain.Role;
import com.zs.domain.User;
import com.zs.service.UserService;


import java.util.List;

/**
 * User表Service实现类
 */
public class UserServiceImpl implements UserService {


    //注入UserDao
    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    private RoleDao roleDao;
    public void setRoleDao(RoleDao roleDao) {
        this.roleDao = roleDao;
    }

    @Override
    public List<User> list() {
        List<User> userList=userDao.findAll();
        for (User user : userList) {
            Long id = user.getId();
            List<Role> roles=roleDao.findRoleById(id);
            user.setRoles(roles);
        }
        return userList;
    }

    @Override
    public void save(User user, Long[] roleIds) {
        //第一步  向sys_user表中存储数据
        Long userId = userDao.save(user);

        //第二部  向sys_user_role 关系表中存储多条数据
        userDao.saveUserRoleRel(userId,roleIds);
    }

    @Override
    public void del(Long userId) {
        //1.删除sys_user_role关系表
        userDao.delUserRoleRel(userId);

        //2.删除sys_user表
        userDao.del(userId);
    }
}
